﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EMR_System.Forms
{
    public partial class PatientForm : Form
    {
        private Patient p;

        public PatientForm()
        {
            InitializeComponent();
            this.Text = "Add New Patient";

            this.p = null;
        }

        public PatientForm(Patient p)
        {
            InitializeComponent();
            this.Text = "Update Patient";

            this.p = p;
            addPatientBtn.Text = "Update";

            // Fill fields for patient update
            firstNameTB.Text = p.FirstName;
            lastNameTB.Text = p.LastName;
            addressLine1TB.Text = p.Address.Split(new char[] { ',' })[0].Trim();
            cityTB.Text = p.Address.Split(new char[] { ',' })[1].Trim();
            stateTB.Text = p.Address.Split(new char[] { ',' })[2].Trim().Split(new char[] { ' ' })[0].Trim();
            zipTB.Text = p.Address.Split(new char[] { ',' })[2].Trim().Split(new char[] { ' ' })[1].Trim();
            phoneTB.Text = p.PhoneNumber;
            ssnTB.Text = p.SSN;
            dobTB.Text = p.DateOfBirth.ToString("MM/dd/yyyy");

            emergencyContactFirstNameTB.Text = p.EmergencyContact.FirstName;
            emergencyContactLastNameTB.Text = p.EmergencyContact.LastName;
            emergencyContactPhoneTB.Text = p.EmergencyContact.PhoneNumber;
            emergencyContactRelationTB.Text = p.EmergencyContact.Relation;

            employerNameTB.Text = p.Employer.Name;
            employerPhoneTB.Text = p.Employer.PhoneNumber;

            insuranceAddressLine1TB.Text = p.Insurance.Address.Split(new char[] { ',' })[0].Trim();
            insuranceCityTB.Text = p.Insurance.Address.Split(new char[] { ',' })[1].Trim();
            insuranceStateTB.Text = p.Insurance.Address.Split(new char[] { ',' })[2].Trim().Split(new char[] { ' ' })[0].Trim();
            insuranceZipTB.Text = p.Insurance.Address.Split(new char[] { ',' })[2].Trim().Split(new char[] { ' ' })[1].Trim();
            insurancePhoneTB.Text = p.Insurance.PhoneNumber;
            insurancePolicyNumberTB.Text = p.Insurance.PolicyNumber;
            insuranceGroupNumberTB.Text = p.Insurance.GroupNumber;
        }

        private void PatientForm_Load(object sender, EventArgs e)
        {
            insuranceProviderCB.Items.Add("BCBS");
            insuranceProviderCB.Items.Add("Principal");
            insuranceProviderCB.Items.Add("Medicare");
            insuranceProviderCB.Items.Add("Medicaid");

            if (p != null)
            {
                if (p.Insurance.Provider == InsuranceProvider.BCBS)
                    insuranceProviderCB.SelectedIndex = insuranceProviderCB.Items.IndexOf("BCBS");
                else if (p.Insurance.Provider == InsuranceProvider.Principal)
                    insuranceProviderCB.SelectedIndex = insuranceProviderCB.Items.IndexOf("Principal");
                else if (p.Insurance.Provider == InsuranceProvider.Medicare)
                    insuranceProviderCB.SelectedIndex = insuranceProviderCB.Items.IndexOf("Medicare");
                else
                    insuranceProviderCB.SelectedIndex = insuranceProviderCB.Items.IndexOf("Medicaid");
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void addPatientBtn_Click(object sender, EventArgs e)
        {
            if (ValidateFields())
            {
                if (addPatientBtn.Text == "Add")
                {
                    InsuranceProvider provider;

                    if (insuranceProviderCB.SelectedItem.Equals("BCBS"))
                        provider = InsuranceProvider.BCBS;
                    else if (insuranceProviderCB.SelectedItem.Equals("Principal"))
                        provider = InsuranceProvider.Principal;
                    else if (insuranceProviderCB.SelectedItem.Equals("Medicare"))
                        provider = InsuranceProvider.Medicare;
                    else
                        provider = InsuranceProvider.Medicaid;

                    string insuranceAddress = insuranceAddress = String.Format("{0}, {1}, {2} {3}", insuranceAddressLine1TB.Text, insuranceCityTB.Text, insuranceStateTB.Text, insuranceZipTB.Text);

                    InsuranceInformation insuranceInfo = new InsuranceInformation(provider, insuranceAddress, insurancePhoneTB.Text, insurancePolicyNumberTB.Text, insuranceGroupNumberTB.Text);
                    EmergencyContact emergencyContact = new EmergencyContact(emergencyContactFirstNameTB.Text, emergencyContactLastNameTB.Text, emergencyContactPhoneTB.Text, emergencyContactRelationTB.Text);
                    EmployerInformation employerInfo = new EmployerInformation(employerNameTB.Text, employerPhoneTB.Text);

                    string patientAddress = patientAddress = String.Format("{0}, {1}, {2} {3}", addressLine1TB.Text, cityTB.Text, stateTB.Text, zipTB.Text);

                    EMR_System.Patient p = new EMR_System.Patient(firstNameTB.Text, lastNameTB.Text, patientAddress, phoneTB.Text, ssnTB.Text, Convert.ToDateTime(dobTB.Text), insuranceInfo, emergencyContact, employerInfo);

                    int patientID = EMR_System.Controllers.PatientController.AddPatient(p);

                    if (patientID == -1)
                    {
                        MessageBox.Show("The patient was not successfully added to the system. Please try again");
                        ResetForm();
                    }
                    else
                    {
                        MessageBox.Show(String.Format("Patient {0} successfully added!", patientID));
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("Update functionality not supprted yet", "Unsupported Operation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private bool ValidateFields()
        {
            List<string> missingFields = new List<string>();

            if (firstNameTB.Text == "") missingFields.Add("Patient First Name");
            if (lastNameTB.Text == "") missingFields.Add("Patient Last Name");
            if (addressLine1TB.Text == "") missingFields.Add("Patient Address");
            if (cityTB.Text == "") missingFields.Add("Patient City");
            if (!stateTB.MaskCompleted) missingFields.Add("Patient State");
            if (!zipTB.MaskCompleted) missingFields.Add("Patient Zip Code");
            if (!phoneTB.MaskCompleted) missingFields.Add("Patient Phone Number");
            if (!ssnTB.MaskCompleted) missingFields.Add("Patient SSN");
            if (!dobTB.MaskCompleted) missingFields.Add("Patient Date of Birth");

            if (emergencyContactFirstNameTB.Text == "") missingFields.Add("Emergency Contact First Name");
            if (emergencyContactLastNameTB.Text == "") missingFields.Add("Emergency Contact Last Name");
            if (!emergencyContactPhoneTB.MaskCompleted) missingFields.Add("Emergency Contact Phone Number");
            if (emergencyContactRelationTB.Text == "") missingFields.Add("Emergency Contact Relation");

            if (employerNameTB.Text == "") missingFields.Add("Employer Name");
            if (!employerPhoneTB.MaskCompleted) missingFields.Add("Employer Phone Number");

            if (insuranceProviderCB.SelectedIndex == -1) missingFields.Add("Insurance Provider");
            if (insuranceAddressLine1TB.Text == "") missingFields.Add("Insurance Provider Address");
            if (insuranceCityTB.Text == "") missingFields.Add("Insurance Provider City");
            if (!insuranceStateTB.MaskCompleted) missingFields.Add("Insurance Provider State");
            if (!insuranceZipTB.MaskCompleted) missingFields.Add("Insurance Provider Zip");
            if (!insurancePhoneTB.MaskCompleted) missingFields.Add("Insurance Provider Phone Number");
            if (insurancePolicyNumberTB.Text == "") missingFields.Add("Insurance Provider Policy Number");
            if (insuranceGroupNumberTB.Text == "") missingFields.Add("Insurance Provider Group Number");

            if (missingFields.Count == 0)
            {
                return true;
            }
            else if (missingFields.Count == 1)
            {
                string message = "The following field is required:\n* " + missingFields[0];
                MessageBox.Show(message);
                return false;
            }
            else
            {
                string message = "The following fields are required:\n";

                for (int i = 0; i < missingFields.Count - 1; i++)
                {
                    message += "* " + missingFields[i] + "\n";
                }

                message += "* " + missingFields[missingFields.Count - 1];

                MessageBox.Show(message);

                return false;
            }
        }

        private void ResetForm()
        {
            #region Reset Patient Information
            foreach (Control c in patientDemographics.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
                else if (c is MaskedTextBox)
                    ((MaskedTextBox)c).Text = "";
            }
            #endregion

            #region Reset Emergency Contact Information
            foreach (Control c in emergencyContactInformation.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
                else if (c is MaskedTextBox)
                    ((MaskedTextBox)c).Text = "";
            }
            #endregion

            #region Reset Employer Information
            foreach (Control c in employerInformation.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
                else if (c is MaskedTextBox)
                    ((MaskedTextBox)c).Text = "";
            }
            #endregion

            #region Insurance Information
            foreach (Control c in insuranceInformation.Controls)
            {
                if (c is TextBox)
                    ((TextBox)c).Text = "";
                else if (c is MaskedTextBox)
                    ((MaskedTextBox)c).Text = "";
                else if (c is ComboBox)
                    ((ComboBox)c).SelectedIndex = -1;
            }
            #endregion

            firstNameTB.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ResetForm();
        }
    }
}
